<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2021-12-13 11:51:18
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-07 09:00:16
-->
<template>
  <div class="app-container">
    <div class="header">
      <div class="bread">
        <span style="margin-left: 10px">商机状态组设置</span>
      </div>
      <div>
        <el-button type="primary" size="default" @click="newOpt">
          <span>新建</span>
        </el-button>
      </div>
    </div>
    <div class="content">
      <el-table :data="data" :height="tableHight">
        <el-table-column label="商机组名称" prop="name"> </el-table-column>
        <el-table-column label="创建人" prop="userName"> </el-table-column>
        <el-table-column label="创建时间" prop="createTime"> </el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            {{ scope.row.status == 1 ? "启用" : "停用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <span
              style="margin-right: 10px; color: #1267fa; cursor: pointer"
              @click="edit(scope.row)"
              v-if="scope.row.isDelete !== 1"
              >编辑</span
            >
            <span
              style="margin-right: 10px; color: #1267fa; cursor: pointer"
              @click="del(scope.row)"
              v-if="scope.row.isDelete !== 1"
              >删除</span
            >
            <span
              style="margin-right: 10px; color: #1267fa; cursor: pointer"
              @click="change(scope.row)"
            >
              {{ scope.row.status == 1 ? "停用" : "启用" }}
            </span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        background
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        layout=" ->,total, sizes, prev, pager, next, jumper "
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="新建状态组"
      :visible.sync="statusShow"
      width="50%"
      @close="statusClose"
    >
      <el-form
        :model="statusForm"
        ref="statusForm"
        :rules="statusRules"
        :inline="false"
        size="normal"
      >
        <el-form-item label="商机状态组" prop="comUnit">
          <el-input
            v-model="statusForm.comUnit"
            style="width: 330px"
            placeholder="请输入商机状态组名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div>
        <div class="stageSet">阶段设置</div>
        <div class="table">
          <div class="table-header">
            <div class="table-header-title">阶段</div>
            <div class="table-header-title">阶段名称</div>
            <div class="table-header-title">赢单率</div>
          </div>
          <div class="table-content">
            <div v-for="(item, index) in stageSetData" :key="index">
              <div class="table-content-main">
                <div class="table-content-main-title">阶段</div>
                <div class="table-content-main-title">
                  <el-input
                    v-model="item.stageValue"
                    placeholder="请输入阶段名称"
                  ></el-input>
                </div>
                <div class="table-content-main-title lastIpt">
                  <el-input
                    v-model="item.rate"
                    placeholder="请输入赢单率(百分比)"
                  ></el-input>
                  <i class="iconfont icon-add" @click="newStage"></i>
                  <i
                    class="iconfont icon-close"
                    v-if="index !== 0"
                    @click="reduceStage(index)"
                  ></i>
                </div>
              </div>
            </div>
            <div v-for="item in endData" :key="item.id">
              <div class="table-content-main">
                <div class="table-content-main-title">完结</div>
                <div class="table-content-main-title">
                  <el-input
                    v-model="item.label"
                    placeholder="请输入阶段名称"
                    disabled
                  ></el-input>
                </div>
                <div class="table-content-main-title lastIpt">
                  <el-input
                    v-model="item.value"
                    placeholder="请输入赢单率(百分比)"
                    disabled
                  ></el-input>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer">
        <el-button @click="statusShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  optStatusList,
  deleteOptStatus,
  insert,
  chaStatus,
  groupList,
  updateInsert,
} from "@/api/sys";
export default {
  data() {
    return {
      tableHight: window.innerHeight - 330,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      submit: true,
      data: [],
      stageSetData: [
        {
          stageValue: "",
          rate: "",
        },
      ],
      statusId: "",
      endData: [
        {
          id: 121,
          value: 100,
          label: "赢单",
        },
        {
          id: 131,
          value: 0,
          label: "输单",
        },
        {
          id: 141,
          value: 0,
          label: "无效",
        },
      ],
      statusShow: false,
      statusForm: {
        comUnit: "",
      },
      statusRules: {
        comUnit: [
          { required: true, message: "请输入商机状态组", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.tableHight = window.innerHeight - 330;
      })();
    };
  },
  activated() {
    this.tableHight = window.innerHeight - 330;
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
    //   dialog关闭
    statusClose() {
      this.stageSetData = [
        {
          stageValue: "",
          rate: "",
        },
      ];
    },
    //   查询商机组列表
    getGroupList(id) {
      groupList({ id: id }).then((res) => {
        this.stageSetData = res.data;
      });
    },
    // 新增商机状态组
    onSubmit() {
      this.$refs.statusForm.validate((valid) => {
        if (!valid) return;
        for (var i = 0; i < this.stageSetData.length; i++) {
          if (this.stageSetData[i].stageValue == "") {
            console.log(this.stageSetData[i]);
            return this.$message.info("请填写或者删除已添加的阶段");
          }
        }
        var fd = new FormData();
        fd.append("name", this.statusForm.comUnit);
        fd.append("crmOpportunityStatus", JSON.stringify(this.stageSetData));
        if (this.submit == true) {
          insert(fd).then((res) => {
            this.$message.success(res.msg);
            this.statusShow = false;
            this.getList();
          });
        } else {
          fd.append("id", this.statusId);
          updateInsert(fd).then((res) => {
            this.$message.success(res.msg);
            this.statusShow = false;
            this.getList();
            this.submit = true;
          });
        }
      });
    },
    // 新增阶段设置
    newStage() {
      let obj = {
        stageValue: "",
        rate: "",
      };
      this.stageSetData.push(obj);
      console.log(this.stageSetData);
    },
    // 删除阶段设置
    reduceStage(i) {
      this.stageSetData.splice(i, 1);
    },
    // 编辑
    edit(row) {
      this.statusId = row.id;
      this.statusForm.comUnit = row.name;
      this.statusShow = true;
      this.submit = false;
      this.getGroupList(row.id);
    },
    // 删除
    async del(row) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商机状态组, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        deleteOptStatus({
          id: row.id,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
        });
      }
      // console.log('确认了删除')
    },
    // 改变状态
    async change(row) {
      const confirmResult = await this.$confirm(
        `此操作将永久${
          row.status == 1 ? "停用" : "启用"
        }该商机状态组, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info(
          `已取消${row.status == 1 ? "停用" : "启用"}!`
        );
      } else {
        let status = "";
        if (row.status == 1) {
          status = 2;
        } else if (row.status == 2) {
          status = 1;
        }
        chaStatus({ id: row.id, status: status }).then((res) => {
          this.$message.success(res.msg);
          this.getList();
        });
      }
    },
    //   新增商机组
    newOpt() {
      this.statusShow = true;
    },
    //   获取列表
    getList() {
      optStatusList({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      }).then((res) => {
        this.data = res.rows;
        this.total = res.total;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ececec;
  .bread {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #2b2b2b;
  }
}
.stageSet {
  font-size: 18px;
  color: #2b2b2b;
  font-weight: 700;
}
.table {
  height: 500px;
  overflow: auto;
  .table-header {
    margin-top: 10px;
    display: flex;
    width: 100%;
    justify-content: space-around;
    border: 1px solid #ececec;
    height: 40px;
    line-height: 40px;
    .table-header-title {
      text-align: center;
      width: 200px;
    }
  }
  .table-content {
    .table-content-main {
      display: flex;
      justify-content: space-around;
      height: 50px;
      align-items: center;
      border: 1px solid #ececec;
      border-top: none;
      .table-content-main-title {
        text-align: center;
        width: 240px;
      }
      .lastIpt {
        display: flex;
        align-items: center;
        i {
          margin-left: 10px;
        }
      }
    }
  }
}
.el-pagination {
  margin-top: 24px;
  /deep/ .el-pagination__total {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    margin-right: 25px;
  }
  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-input__inner {
    // width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .more {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-pagination__jump {
    .el-input__inner {
      width: 40px;
      height: 40px;
    }
  }
}
</style>